﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;//添加引用
using SHDocVw;//添加引用，COM, Microsoft Internet Controls. C:\Windows\system32\shdocvw.dll
using mshtml;//添加引用，COM, Microsoft HTML Object Library. C:\WINDOWS\system32\mshtml.tlb

using WebBrowser = SHDocVw.WebBrowser;
namespace IBP2
{
    //必须加上下面的ComVisible(true)等，guid用自己生成的
    [ComVisible(true), ClassInterface(ClassInterfaceType.None), Guid("1F49CDF3-6CD4-46EF-AA47-C41B782AF49F")]
    public class BHO2 : IObjectWithSite
    {
        WebBrowser webBrowser;
        HTMLDocument document;

        public void OnDocumentComplete(object pDisp, ref object URL)
        {            
            try
            {
                document = (HTMLDocument)webBrowser.Document;

                //foreach (IHTMLInputElement input in document.getElementsByTagName("INPUT"))
                //{
                //    string name = input.name == null ? "name is null" : input.name;
                //    IHTMLElement ele = (IHTMLElement)input;
                //    string id = ele.id == null ? "id is null" : ele.id;
                //    MessageBox.Show("id=" + id + ", name=" + name + ", type=" + input.type );
                //}
                
                string content = "old=" + document.title + "\r\n";
                document.title = "OK = " + document.title;
                content += "new=" + document.title;
                content += "\r\n url=" + document.url;
                File.WriteAllText("D:\\IBP.txt", content);                
            }
            catch (Exception ex)
            {
                File.WriteAllText("D:\\IBP.txt", ex.Message);
            }
        }
        public int SetSite(object site)
        {
            if (site != null)
            {
                webBrowser = (WebBrowser)site;
                webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
            }
            else
            {
                webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
                webBrowser = null;
            }
            
            return 0;
        }

        public int GetSite(ref Guid guid, out IntPtr ppvSite)
        {
            IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
            int h = Marshal.QueryInterface(punk, ref guid, out ppvSite);
            Marshal.Release(punk);

            return h;
        }

        public static string keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects";
        [ComRegisterFunction]
        public static void RegisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(keyName, true);
            if (registryKey == null)
            {
                registryKey = Registry.LocalMachine.CreateSubKey(keyName, RegistryKeyPermissionCheck.ReadWriteSubTree);
            }
            string guid = type.GUID.ToString("B");
            RegistryKey ourKey = registryKey.OpenSubKey(guid, true);
            if (ourKey == null)
            {
                ourKey = registryKey.CreateSubKey(guid, RegistryKeyPermissionCheck.ReadWriteSubTree);
            }
            ourKey.SetValue("NoExplorer", 1);
            ourKey.SetValue("Alright", 1);
            ourKey.SetValue("test", "test1734");
            registryKey.Close();
            ourKey.Close();
        }

        [ComUnregisterFunctionAttribute]
        public static void UnregisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(keyName, true);
            string guid = type.GUID.ToString("B");
            if (registryKey != null)
            {
                registryKey.DeleteSubKey(guid, false);
            }
        }
    }
}
